#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import numpy as np
import cv2

def _extract(f):
    img = cv2.imread(f)[:,45:]
    oriImg = img.copy()
    grayImg = cv2.cvtColor(oriImg, cv2.COLOR_BGR2GRAY)
    binImg = cv2.threshold(grayImg, 127, 1, cv2.THRESH_BINARY_INV)[1]
    blurImg = cv2.blur(binImg, (2,2))
    s = blurImg.sum(axis=0)
    blurImg[:, s < 9] = np.zeros((blurImg.shape[0], 1), dtype=np.uint8)
    contours, _ = cv2.findContours(blurImg, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    rects = [cv2.boundingRect(c) for c in contours]
    rects.sort(key=lambda (x,y,w,h): x)
    ps = [img[y:y+h,x:x+w] for x,y,w,h in rects if w > 10 and h > 15]
    return ps